<%@page import="org.json.JSONArray"%>
<%@page import="org.json.JSONObject"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
   <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<link rel='stylesheet' type='text/css' href='${initParam.root }/css/mentoring.css' />
<c:set value="${sessionScope.mvo }" var="mvo"></c:set>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.0.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
<script>
var nowPage;

	$(function(){
			showList(${requestScope.statusMap.lvo}.list,${requestScope.statusMap.lvo}.pagingBean);
			showRList(${requestScope.statusMap.receiveRList});
			showSList(${requestScope.statusMap.sendRList});

		
		
		$(":radio[name=statusType]").change(function(){
			statusAjax("1");				
		});
		
		var tr=null;
		
		$("#questionTable tbody").on("click","tr:even",function(){
			$(this).children("td").css("color","#6E6E6E");
			if(tr!=null){
				tr.next().hide();
				if(tr.html()==$(this).html()){
					tr=null;
					return;
				}
			}
			tr=$(this);
				$.ajax({
					type:"get",
					url:"${initParam.root}/mentoring/viewContent.do",
					data:"no="+$(this).children("td:first-child").find(":hidden[name=no]").val(),
					dataType:"json",
					success:function(data){
						tr.next().children("td").eq(1).html(data.content);
						var str="Not read";
						if(data.receiveDate!=null)
							str="Receive Date : <br>"+data.receiveDate;
							tr.next().children("td").eq(2).html(str);
							tr.next().show();
					}
				});
					
		}).on("click",".deleteConfirm",function(e){
			if(confirm("해당글을 삭제하시겠습니까?")){
				$.ajax({
					url:"${initParam.root }/mentoring/deleteQuestion.do",
					data:$(this).find(":hidden").val()+"&"+getStatus()+"&page="+nowPage,
					dataType:"json",
					success:function(lvo){
						showList(lvo.list,lvo.pagingBean);
					}
				});
				
			}
		}).on("click",".rejectBtn",function(){
			if(confirm("거절하시겠습니까?")){
				$.ajax({
					url:"${initParam.root}/mentoring/rejectQuestion.do",
					data:getStatus()+"&page="+nowPage+"&no="+$(this).parents("tr").prev().children("td:first-child").find(":hidden[name=no]").val(),
					dataType:"json",
					success:function(lvo){
						showList(lvo.list,lvo.pagingBean);
					}
				});
			} 
		}).on("click",".replyQuestion",function(){
			window.open("${initParam.root }/mentoring/replyView.do?no="+$(this).parents("tr").prev().children("td:first-child").find(":hidden[name=no]").val(), "popup",
			"width=400,height=280,top=50,left=400");
		}).on("click",".requestMentor",function(){
			if(confirm("멘토를 신청하시겠습니까?")){
				$.ajax({
					url:"${initParam.root}/mentoring/sendMentorRequest.do",
					data:"no="	+$(this).parents("tr").prev().children("td:first-child").find(":hidden[name=no]").val(),
					dataType:"json",
					success:function(data){
						if(data.ame!=null){
							alert(data.ame);
						}else{
						showSList(data.list);
						}
					}
				});
				$(this).remove();

			}
		});
		
		$("#pb").on("click",".page",function(){
			
			statusAjax($(this).attr("href"));
			return false;
		});
		$("#rBody").on("click",".rejectRequest",function(){
			if(confirm("멘토신청을 거절하시겠습니까?")){
				$.ajax({
					url:"${initParam.root}/mentoring/replyReceivedRequest.do",
					data:"agree=-1&mentor.id=${sessionScope.mvo.id}&mentee.id="+$(this).parents("tr").children("td:first-child").text(),
					dataType:"json",
					success:function(data){
						showRList(data);
					}
					
				});
			}
		}).on("click",".agreeRequest",function(){
			if(confirm("멘토신청을 수락하시겠습니까?")){
				$.ajax({
				url:"${initParam.root}/mentoring/replyReceivedRequest.do",
				data:"agree=1&mentor.id=${sessionScope.mvo.id}&mentee.id="+$(this).parents("tr").children("td:first-child").text(),
				dataType:"json",
				success:function(data){
					showRList(data);
				}				
			});
			}
		});
		$("#sBody").on("click",".cancelRequest",function(){
			if(confirm("멘토신청을 취소하시겠습니까?")){
				$.ajax({
					url:"${initParam.root}/mentoring/cancelRequest.do",
					data:"agree=-1&mentee.id=${sessionScope.mvo.id}&mentor.id="+$(this).parents("tr").children("td:first-child").text(),
					dataType:"json",
					success:function(data){
						showSList(data);
					}
					
				});
			}
		});
		
});//document
	
	
	//Received mentor Request List
	function showRList(data){
		var str="";
		$.each(data,function(i,a){
			str+="<tr><td><font style='font-size: 15px' color='white'>";
			str+=a.id+"</font></td><td>Level."+a.level;
			str+=" <a href='#' class='agreeRequest'>수락</a>";
			str+=" <a href='#' class='rejectRequest'>거절</a></td></tr>";
		});
		$("#rBody").html(str);
	}
	
	//sent mentor Request List
	function showSList(data){
		var str="";
		$.each(data,function(i,a){
			str+="<tr><td><font style='font-size: 15px' color='white'>";
			str+=a.id+"</font></td><td>Level."+a.level;
			str+=" <a href='#' class='cancelRequest'>취소</a></td></tr>";
		});
		$("#sBody").html(str);
	}
	function statusAjax(page){
		$.ajax({
			url:"${initParam.root }/mentoring/statusAjax.do",
			data:getStatus()+"&page="+page,
			dataType:"json",
			success:function(lvo){
				showList(lvo.list,lvo.pagingBean);
			}
		});
	}
	
	function showList(list,pb){
		
		var str="";
		$.each(list,function(i,a){
			if(a.receiver.id=="${sessionScope.mvo.id}"&&a.receiveDate==null){
				str+="<tr class='notRead'><td>";
			}else{
				str+="<tr class='read'><td>";
			}
					
			if(a.reNoteNo!=-1){
				str+="RE:";
			}
			str+=a.refNo;
			str+="<a href=# class=deleteConfirm>x	<input type=hidden name=deleteInfo value=";
			str+="no="+a.no+"&sender.id="+a.sender.id+"&receiver.id="+a.receiver.id+"></a>";
			str+="<input type=hidden value="+a.no +" name=no>";
			str+="</b></td><td><b>"+a.status+"</b></td><td>";
			if(a.reNoteNo!=-1){
				str+="[질문번호 : "+a.refNo+"] 에 대한 답변입니다.";
			}else{
				str+="<p class=contentText><b>"+a.content +"</b></p>";
			}
			str+="</td><td><b>"+a.sendDate +"</b></td>	</tr>";

			str+="	<tr style='display: none;' ><td align=right colspan=2><b>▶";
			if(a.sender.id=="${mvo.id}"&&a.reNoteNo=="-1"){
				str+="Question Level : "+a.level+"<br>";
				if(a.status=="보낸질문(대기중)"){
					str+="Reject : "+(a.refOrder-1);
				}else if(a.status=="보낸질문(답변완료)"){
					str+="Answer : "+a.receiver.id;
				}
			}else if(a.sender.id=="${mvo.id}"&&a.reNoteNo!="-1"){
				str+="Questioner : "+a.receiver.nick+" Level."+a.receiver.level+"<br>";
			}else if(a.receiver.id=="${mvo.id}"&&a.reNoteNo=="-1"){
				str+="Questioner : "+a.sender.nick+" Level."+a.sender.level+"<br>";
					if(a.status=="받은질문(대기중)"){
					str+="Reject : "+(a.refOrder-1)+"<br>";
					str+="	<input type=button value=답변 class=replyQuestion>";
					str+="<input type=button value=거절 class=rejectBtn>";
				}
			}else if(a.status=="답변"){
				str+="Answer : "+a.sender.id+" Level."+a.sender.level+"<br>";
				if(a.reject==0){
					str+="<input type=button value=멘토신청 class='requestMentor'>";
				}
			}
			str+="</b></td><td  style='word-break:break-all;'></td><td></td>";
		});
			$("#qBody").html(str);
			nowPage=pb.nowPage;
			str="";
			if(pb.previousPageGroup)
				str+="<a href='"+(pb.startPageOfPageGroup-1)+"' class='page'><</a>";
 			for(i=pb.startPageOfPageGroup; i<=pb.endPageOfPageGroup;i++){
				if(i==nowPage)
 					str+=" "+i+" ";
				else
					str+="<a href='"+i+"' class='page'> "+i+" </a>";
			}
			if(pb.nextPageGroup)
				str+="<a href='"+(pb.endPageOfPageGroup+1)+"' class='page'>></a>";
			$("#pb").html(str);
		}
	
	function getStatus(){
		var s=$(":radio[name=statusType]:checked").val();
		return "status="+s;
		
	}

	
</script>

<div id="sendRequest">
<table cellpadding="1" width="100%" id="sendTable" >
	<thead>
	<tr>
		<th colspan="2">Sent </th>
	</tr>
	</thead>
	<tbody id="sBody">
	</tbody>
</table>
</div>
<div id="questionStatus" >
<br><br>
<input type="radio" name="statusType" value="all" checked="checked">all &nbsp;&nbsp;&nbsp;
<input type="radio" name="statusType" value="sent">sent &nbsp;&nbsp;&nbsp;
<input type="radio" name="statusType" value="received">received &nbsp;&nbsp;&nbsp;

<br><hr><br>
	<table cellpadding="3" width="100%" id="questionTable">
		<thead><tr valign="top" >
			<th width="13%">No</th>
			<th width="25%">STATUS</th>
			<th width="50%">QUESTION</th>
			<th width="19%">DATE</th>
			</tr></thead>
			<tbody valign="top" id="qBody"></tbody>
		</table>
		<br>
		<div id="pb" align="center"></div>
</div>



<div id="receiveRequest">
<table cellpadding="3" width="100%" id="receiveTable" >
	<thead>
	<tr>
		<th colspan="2">Received</th>
	</tr>
	</thead>
	<tbody id="rBody">
	</tbody>
</table>


</div>
